package com.krt.facenettydemo;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.ImageFormat;
import android.graphics.Rect;
import android.graphics.YuvImage;
import android.media.FaceDetector;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.blankj.utilcode.util.LogUtils;
import com.google.gson.Gson;
import com.krt.facenettydemo.netty.AesUtil;
import com.krt.facenettydemo.netty.NettyClient;
import com.krt.facenettydemo.utils.Base64Util;
import com.telpo.tps550.api.TelpoException;
import com.telpo.tps550.api.idcard.IdCard;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class MainActivity extends AppCompatActivity {

    EditText editText;
    ImageView imageView;

    Handler nettyHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    Toast.makeText(MainActivity.this, "Netty连接成功", Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText = findViewById(R.id.ip);
        imageView = findViewById(R.id.img);
        findViewById(R.id.bind).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(editText.getText().toString())) {
                    Toast.makeText(MainActivity.this, "请先输入ip地址", Toast.LENGTH_SHORT).show();
                    return;
                }
                new Thread(() -> {
                    try {
//                        NettyClient.getInstance().bind(MainActivity.this, "172.30.10.60", nettyHandler);
                        NettyClient.getInstance().bind(MainActivity.this, editText.getText().toString(), nettyHandler);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }).start();
            }
        });
        findViewById(R.id.unbind).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NettyClient.getInstance().unBind();
            }
        });
        findViewById(R.id.face).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startCamera(105);
            }
        });
        findViewById(R.id.send).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, FaceVerActivity.class));
            }
        });
//        receiveMessage();
    }


    private String getInfo() {
        String json = "";
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("msg", "成功");
            jsonObject.put("code", "0");
            jsonObject.put("data", 1);
            jsonObject.put("timestamp", System.currentTimeMillis());
            Log.w("marcus", jsonObject.toString());
            json = AesUtil.getAESEncrypt(jsonObject.toString(), "QKhyyuXe37SGjMkT");
            return json;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    private void startCamera(int type) {
        String path = Environment.getExternalStorageDirectory() + File.separator + "facePic" + File.separator + System.currentTimeMillis() + ".jpg";
        File filePath = new File(path);
        try {
            if (!filePath.exists()) {
                filePath.getParentFile().mkdirs();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        Uri imageUri;
        /**
         * android7.0及以上
         */
        if (Build.VERSION.SDK_INT >= 24) {
            imageUri = FileProvider.getUriForFile(this, getPackageName() + ".fileprovider", filePath);
        } else {
            imageUri = Uri.fromFile(filePath);
        }
        //启动相机程序
        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
        intent.putExtra("camerasensortype", 2);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
        startActivityForResult(intent, type);

    }



}
